package com.mall.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mall.annotation.OperationLog;
import com.mall.db.domain.Member;
import com.mall.db.service.MemberService;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/system/mc")
public class MemberController extends BaseController<Member> {

    @Resource(name="memberService")
    private MemberService ms;



    @Override
    @OperationLog(value = "查询系统成员",type = 0)
    public Map<String, Object> find(Member member) {
        //实现分页
        PageHelper.startPage(member.getPage(),member.getLimit());

        QueryWrapper<Member> qw = new QueryWrapper<>();
        List<Member> list = ms.list(qw);

        PageInfo<Member> page = new PageInfo<>(list,member.getLimit());

        return this.findMap(page.getList(), page.getTotal());
    }



    @Override
    @OperationLog(value = "新增系统成员",type = 0)
    public Map<String, Object> add(Member member) {
        //TODO 密码加密
        boolean b = ms.save(member);
        return this.iud("新增",b,member);
    }

    @Override
    @OperationLog(value = "编辑系统成员",type = 0)
    public Map<String, Object> update(Member member) {
        boolean b = ms.updateById(member);
        return this.iud("编辑",b,member);
    }

    @RequestMapping("/editPwd")
    @OperationLog(value = "修改登录密码",type = 1)
    public Map<String,Object> editPwd(@RequestParam(name="oldPwd",required = false) String oldPwd,
                                      @RequestParam(name="newPwd") String newPwd,
                                      @RequestParam(name="code") String code,
                                      @RequestParam(name="id",required = true) int id,
                                      @RequestParam(name="email",required = false) String email,
                                      HttpSession session){
        Map<String,Object> map = new HashMap<>();

        String mobileCode = session.getAttribute("mobileCode")+"";
        session.removeAttribute("mobileCode");
        if(!mobileCode.equals(code)){
            map.put("msg","验证码错误!");
            map.put("is",false);
        }
        Member m = null;
        if(StringUtils.isEmpty(email)) {
            //通过id获取对象
            m = ms.getById(id);
        }else{
            QueryWrapper<Member> qw = new QueryWrapper<>();
            qw.eq("id",id);
            qw.eq("email",email);
            m = ms.getOne(qw);
        }
        if(m!=null){
            if(StringUtils.isEmpty(email) && oldPwd.equals(m.getPassword())){
                m.setPassword(newPwd);//TODO 密码加密
                map.put("is",ms.updateById(m));
                map.put("msg","修改成功");
            }else if (!StringUtils.isEmpty(email)){
                m.setPassword(newPwd);//TODO 密码加密
                map.put("is",ms.updateById(m));
                map.put("msg","修改成功");
            }else{
                map.put("msg","旧密码输入错误!");
                map.put("is",false);
            }
        }else{
            map.put("msg","用户信息过期或不存在");
            map.put("is",false);
        }

        return map;
    }
}
